//
//  PopularForecastFromTableViewCell.m
//  Gxj
//
//  Created by 刘伟 on 2017/9/13.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "PopularForecastFromTableViewCell.h"

@implementation PopularForecastFromTableViewCell

//// 重写表格位置，使每一行cell都分隔开
//- (void)setFrame:(CGRect)frame{
//    //    frame.origin.x += 10;
//    frame.origin.y += 10;
//    frame.size.height -= 10;
//    //    frame.size.width -= 20;
//    [super setFrame:frame];
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.userHeaderView addSubview:self.userHeader]; // 用户头像
        [self.contentView addSubview:self.userHeaderView];// 用户头像背景
        [self.contentView addSubview:self.userName];// 用户名称
        [self.shadowView addSubview:self.userProfile]; // 用户简介
        [self.contentView addSubview:self.timeImage]; // 时间图标
        [self.contentView addSubview:self.timeLab]; // 时间标签
        [self.contentView addSubview:self.meetingImage]; // 会议图片
        [self.meetingImage addSubview:self.industryLab]; // 行业标签
        [self.contentView addSubview:self.grabNumber]; // 抢位人数量
        [self.contentView addSubview:self.gotoBtn]; // 进入会议按钮
        [self.contentView addSubview:self.shadowView]; // 简介背景
        [self.meetingImage addSubview:self.title];  // 标题
        [self.contentView addSubview:self.browseThroughImage];  // 浏览图标
        [self.contentView addSubview:self.browseThroughLab];   // 浏览标签
        [self.contentView addSubview:self.thumpUpImage];   // 点赞图标
        [self.contentView addSubview:self.thumpUpLab];   // 点赞数量
        [self.contentView addSubview:self.gradeImage];   // 等级图标
        [self.contentView addSubview:self.gradeLab];   // 等级标签
        [self.contentView addSubview:self.guanzhuBtn]; // 关注按钮
        [self.contentView addSubview:self.bottomBackColor]; // 底部背景
        [self.meetingImage addSubview:self.starView];
        [self.starView addSubview:self.startImage];   // 开始时间图标
        [self.starView addSubview:self.startLab];     // 开始时间标签
    }
    return self;
}

- (void)setModel:(PopularForecastFromModel_Two *)model
{
    [_userHeader sd_setImageWithURL:[NSURL URLWithString:model.head_img] forState:0 placeholderImage:[UIImage imageNamed:@"toixiang"]];
    _userName.text = model.nickname;
    _userProfile.text = [NSString stringWithFormat:@"简介：%@",model.info];
    [KQlable changeLineSpaceForLabel:_userProfile WithSpace:kHeightScale(3)];
    
    if (model.imgsrc.count) {
        [_meetingImage sd_setImageWithURL:model.imgsrc[0]];;
    }
    _title.text = model.title;
    _timeLab.text = [self timeWithTimeIntervalString:[model.create_date stringValue]];
    _thumpUpLab.text = [model.supportnum stringValue];
    _browseThroughLab.text = [model.viewnum stringValue];
    if (model.position.count) {
        _grabNumber.text = [NSString stringWithFormat:@"席位(%ld/18)",model.position.count];
    }else{
        _grabNumber.text = @"席位(0/18)";
    }
    
    [self showMeetingTheStormView:model];
    [_timeLab updateLayout];
    [_browseThroughLab updateLayout];
}

// 时间戳转换
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    NSTimeInterval interval    =[timeString doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM.dd HH:mm"]; // yyyy-MM-dd HH:mm:ss
    NSString *dateString       = [formatter stringFromDate: date];
    
    return dateString;
}

- (void)showMeetingTheStormView:(PopularForecastFromModel_Two *)model
{
    _userHeaderView.sd_layout
    .topSpaceToView(self.contentView,kHeightScale(11))
    .leftSpaceToView(self.contentView,kWidthScale(15))
    .widthIs(kWidthScale(25))
    .heightIs(kHeightScale(25));
    
    _userHeader.sd_layout
    .topSpaceToView(self.userHeaderView,kHeightScale(1.5))
    .leftSpaceToView(self.userHeaderView,kWidthScale(1.5))
    .rightSpaceToView(self.userHeaderView,kWidthScale(1.5))
    .bottomSpaceToView(self.userHeaderView,kHeightScale(1.5));
    
    _userName.sd_layout
    .topSpaceToView(self.contentView, kHeightScale(11))
    .leftSpaceToView(self.userHeaderView,kWidthScale(5))
    .heightIs(kHeightScale(15));
    [_userName setSingleLineAutoResizeWithMaxWidth:kWidthScale(100)];
    
    _gradeImage.sd_layout
    .topSpaceToView(self.userName,kHeightScale(3))
    .leftEqualToView(self.userName)
    .widthIs(kWidthScale(11))
    .heightIs(kHeightScale(11));
    
    _gradeLab.sd_layout
    .centerYEqualToView(self.gradeImage)
    .leftSpaceToView(self.gradeImage,kWidthScale(3))
    .autoHeightRatio(0);
    [_gradeLab setSingleLineAutoResizeWithMaxWidth:kWidthScale(100)];
    
    _guanzhuBtn.sd_layout
    .centerYEqualToView(self.userHeaderView)
    .rightSpaceToView(self.contentView,kWidthScale(15))
    .widthIs(kWidthScale(44))
    .heightIs(kHeightScale(25));
    
    _timeImage.sd_layout
    .topSpaceToView(self.userHeaderView,kHeightScale(5))
    .leftEqualToView(self.userHeaderView)
    .widthIs(kWidthScale(13))
    .heightIs(kHeightScale(13));
    
    _timeLab.sd_layout
    .centerYEqualToView(self.timeImage)
    .leftSpaceToView(self.timeImage,kWidthScale(5))
    .autoHeightRatio(0);
    [_timeLab setSingleLineAutoResizeWithMaxWidth:kWidthScale(100)];
    
    _browseThroughImage.sd_layout
    .centerYEqualToView(self.timeImage)
    .leftSpaceToView(self.timeLab,kWidthScale(10))
    .widthIs(kWidthScale(13))
    .heightIs(kHeightScale(8.6));
    
    _browseThroughLab.sd_layout
    .centerYEqualToView(self.timeImage)
    .leftSpaceToView(self.browseThroughImage,kWidthScale(5))
    .autoHeightRatio(0);
    [_browseThroughLab setSingleLineAutoResizeWithMaxWidth:kWidthScale(50)];
    
    _thumpUpImage.sd_layout
    .centerYEqualToView(self.timeImage)
    .leftSpaceToView(self.browseThroughLab,kWidthScale(10))
    .widthIs(kWidthScale(13))
    .heightIs(kHeightScale(13));
    
    _thumpUpLab.sd_layout
    .centerYEqualToView(self.timeImage)
    .leftSpaceToView(self.thumpUpImage,kWidthScale(5))
    .autoHeightRatio(0);
    [_thumpUpLab setSingleLineAutoResizeWithMaxWidth:kWidthScale(100)];
    
    _meetingImage.sd_layout
    .topSpaceToView(self.timeImage,kHeightScale(5))
    .leftEqualToView(self.userHeaderView)
    .rightSpaceToView(self.contentView,kWidthScale(15))
    .heightIs(kHeightScale(124));
    
    _shadowView.sd_layout
    .topSpaceToView(self.meetingImage,kHeightScale(10))
    .leftEqualToView(self.userHeaderView)
    .rightEqualToView(self.guanzhuBtn)
    .heightIs(kHeightScale(49));
    
    _userProfile.sd_layout
    .topEqualToView(self.shadowView)
    .leftEqualToView(self.shadowView)
    .rightEqualToView(self.shadowView)
    .bottomEqualToView(self.shadowView);
    
    _title.sd_layout
    .bottomEqualToView(self.meetingImage)
    .leftEqualToView(self.meetingImage)
    .rightEqualToView(self.meetingImage)
    .heightIs(kHeightScale(20));
    
    _industryLab.sd_layout
    .topSpaceToView(self.meetingImage,kHeightScale(6))
    .leftSpaceToView(self.meetingImage,kWidthScale(5))
    .widthIs(kWidthScale(35))
    .heightIs(kHeightScale(15));
    
    _grabNumber.sd_layout
    .topSpaceToView(self.shadowView,kHeightScale(10))
    .leftEqualToView(self.shadowView)
    .heightIs(kHeightScale(10));
    [_grabNumber setSingleLineAutoResizeWithMaxWidth:kWidthScale(100)];
    
    _gotoBtn.sd_layout
    .centerYEqualToView(self.grabNumber)
    .rightEqualToView(self.meetingImage)
    .widthIs(kWidthScale(56))
    .heightIs(kHeightScale(20));
    
    _bottomBackColor.sd_layout
    .bottomEqualToView(self.contentView)
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightIs(kHeightScale(5));
    
    for (int i = 0; i < model.position.count; i++) {
        _grabHeader = [[UIImageView alloc] init];
        _grabHeader.frame = CGRectMake(kWidthScale(90) + (kWidthScale(20)*i), kHeightScale(247), kWidthScale(23), kHeightScale(23));
        _grabHeader.layer.cornerRadius = kWidthScale(12);
        _grabHeader.layer.masksToBounds = YES;
        if (model.position.count) {
            [_grabHeader sd_setImageWithURL:[model.position[i] objectForKey:@"head_img"]];;
        }
        
        if (i == 4) {
            // 最后一个加号按钮 可加手势触发事件
        }
        
        [self.contentView addSubview:self.grabHeader]; // 抢位人头像
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
// 头像
- (UIButton *)userHeader
{
    if (!_userHeader)
    {
        _userHeader = [UIButton buttonWithType:UIButtonTypeCustom];
        [_userHeader setImage:[UIImage imageNamed:@"toixiang"] forState:0];
        _userHeader.layer.cornerRadius = 12;
        _userHeader.layer.masksToBounds = YES;
    }
    return _userHeader;
}
// 头像背景
- (UIView *)userHeaderView
{
    if (!_userHeaderView)
    {
        _userHeaderView=[[UIView alloc] init];
        _userHeaderView.backgroundColor = [UIColor colorWithRed:(213.0/255) green:(213.0/255) blue:(213.0/255) alpha:1];;
        _userHeaderView.layer.masksToBounds=YES;
        _userHeaderView.layer.cornerRadius= kWidthScale(12.5);
        _userHeaderView.layer.shadowColor = [UIColor blackColor].CGColor;
        _userHeaderView.layer.shadowOffset = CGSizeMake(-3, 1);
        _userHeaderView.layer.shadowOpacity = 0.4;
        _userHeaderView.layer.shadowRadius = 2.0;
        _userHeaderView.clipsToBounds = NO;
    }
    return _userHeaderView;
}
// 用户名称
- (UILabel *)userName
{
    if (!_userName)
    {
        _userName = [[UILabel alloc] init];
        _userName.text = @"从不后悔的我";
        _userName.textColor = [UIColor colorWithHexString:@"#333333"];
        _userName.font = [UIFont systemFontOfSize:kWidthScale(13)];
        [_userName updateLayout];
    }
    return _userName;
}
// 简介
- (KQlable *)userProfile
{
    if (!_userProfile)
    {
        _userProfile = [[KQlable alloc] init];
        _userProfile.text = @"简介：信仰就是信仰，自由就是自由，工作就是工作，世界就是世界。。。";
        _userProfile.textColor = [UIColor colorWithHexString:@"#666666"];
        _userProfile.font = [UIFont systemFontOfSize:kWidthScale(11)];
        _userProfile.numberOfLines = 0;
        _userProfile.backgroundColor = [UIColor colorWithRed:251.0/255.0 green:251.0/255.0 blue:251.0/255.0 alpha:1];
        _userProfile.layer.cornerRadius = kWidthScale(12);
        _userProfile.layer.masksToBounds = YES;
        [KQlable changeLineSpaceForLabel:_userProfile WithSpace:5.0f];
    }
    return _userProfile;
}
// 时间图标
- (UIImageView *)timeImage
{
    if (!_timeImage)
    {
        _timeImage = [[UIImageView alloc] init];
        _timeImage.image = [UIImage imageNamed:@"time"];
    }
    return _timeImage;
}
// 时间
- (UILabel *)timeLab
{
    if (!_timeLab)
    {
        _timeLab = [[UILabel alloc] init];
        _timeLab.text = @"2017.09.17";
        //_timeLab.textColor = [UIColor colorWithRed:221.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1];
        _timeLab.textColor = [UIColor colorWithHexString:@"#666666"];
        _timeLab.font = [UIFont systemFontOfSize:kWidthScale(11)];
        [_timeLab updateLayout];
    }
    return _timeLab;
}
// 会议图片
- (UIImageView *)meetingImage
{
    if (!_meetingImage)
    {
        _meetingImage = [[UIImageView alloc] init];
        _meetingImage.image = [UIImage imageNamed:@"ditu"];
        _meetingImage.layer.cornerRadius = 12;
        _meetingImage.layer.masksToBounds = YES;
    }
    return _meetingImage;
}
// 行业标签
- (UILabel *)industryLab
{
    if (!_industryLab)
    {
        _industryLab = [[UILabel alloc] init];
        _industryLab.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        _industryLab.text = @"商业";
        _industryLab.textColor = [UIColor whiteColor];
        _industryLab.layer.cornerRadius = 8;
        _industryLab.layer.masksToBounds = YES;
        _industryLab.font = [UIFont systemFontOfSize:kWidthScale(9)];
        _industryLab.textAlignment = NSTextAlignmentCenter;
    }
    return _industryLab;
}
// 抢位人数量
- (UILabel *)grabNumber
{
    if (!_grabNumber)
    {
        _grabNumber = [[UILabel alloc] init];
        _grabNumber.textColor = [UIColor colorWithHexString:@"#666666"];
        _grabNumber.font = [UIFont systemFontOfSize:kWidthScale(12)];
        _grabNumber.text = @"席位(18/18)";
        
        [_grabNumber updateLayout];
    }
    return _grabNumber;
}
//  抢位
- (UIButton *)gotoBtn
{
    if (!_gotoBtn)
    {
        _gotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_gotoBtn setImage:[UIImage imageNamed:@"huiyiqiangwei"] forState:0];
        _gotoBtn.layer.cornerRadius = 10;
        _gotoBtn.layer.masksToBounds = YES;
    }
    return _gotoBtn;
}
// 底部背景
- (UILabel *)bottomBackColor
{
    if (!_bottomBackColor)
    {
        _bottomBackColor = [[UILabel alloc] init];
        _bottomBackColor.backgroundColor = [UIColor colorWithRed:242.0/255.0 green:242.0/255.0 blue:242.0/255.0 alpha:1];
    }
    return _bottomBackColor;
}
// 标题
- (UILabel *)title
{
    if (!_title)
    {
        _title = [[UILabel alloc] init];
        _title.textColor = [UIColor whiteColor];
        _title.text = @"什么时候觉得，数据分析真的非常有用";
        _title.font = [UIFont systemFontOfSize:kWidthScale(13)];
        _title.textAlignment = NSTextAlignmentCenter;
        _title.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    }
    return _title;
}
// 浏览图标
- (UIImageView *)browseThroughImage
{
    if (!_browseThroughImage)
    {
        _browseThroughImage = [[UIImageView alloc] init];
        _browseThroughImage.image=[UIImage imageNamed:@"liulans"];
    }
    return _browseThroughImage;
}
// 浏览标签
- (UILabel *)browseThroughLab
{
    if (!_browseThroughLab)
    {
        _browseThroughLab = [[UILabel alloc] init];
        _browseThroughLab.text = @"13";
        _browseThroughLab.textColor = [UIColor colorWithHexString:@"#999999"];
        _browseThroughLab.font = [UIFont systemFontOfSize:kWidthScale(12)];
        
        [_browseThroughLab updateLayout];
    }
    return _browseThroughLab;
}
// 点赞图标
- (UIImageView *)thumpUpImage
{
    if (!_thumpUpImage)
    {
        _thumpUpImage = [[UIImageView alloc] init];
        _thumpUpImage.image = [UIImage imageNamed:@"zans"];
    }
    return _thumpUpImage;
}
// 点赞数量
- (UILabel *)thumpUpLab
{
    if (!_thumpUpLab)
    {
        _thumpUpLab = [[UILabel alloc] init];
        _thumpUpLab.text = @"1111";
        _thumpUpLab.textColor = [UIColor colorWithHexString:@"#999999"];
        _thumpUpLab.font = [UIFont systemFontOfSize:kWidthScale(12)];
        
        [_thumpUpLab updateLayout];
    }
    return _thumpUpLab;
}
// 简介背景
- (UIView *)shadowView
{
    if (!_shadowView)
    {
        _shadowView = [[UIView alloc] init];
        _shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
        _shadowView.layer.shadowOffset = CGSizeMake(0, 1);
        _shadowView.layer.shadowOpacity = 0.2;
        _shadowView.clipsToBounds = NO;
    }
    return _shadowView;
}
// 关注按钮
- (UIButton *)guanzhuBtn
{
    if (!_guanzhuBtn)
    {
        _guanzhuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_guanzhuBtn setBackgroundImage:[UIImage imageNamed:@"guanzhu"] forState:0];
        _guanzhuBtn.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _guanzhuBtn;
}
// 等级勋章
- (UIImageView *)gradeImage
{
    if (!_gradeImage)
    {
        _gradeImage = [[UIImageView alloc] init];
        _gradeImage.image=[UIImage imageNamed:@"dengjis"];
    }
    return _gradeImage;
}
// 等级标签
- (UILabel *)gradeLab
{
    if (!_gradeLab)
    {
        _gradeLab = [[UILabel alloc] init];
        _gradeLab.text = @"1级";
        _gradeLab.textColor = [UIColor colorWithHexString:@"#999999"];
        _gradeLab.font = [UIFont systemFontOfSize:kWidthScale(11)];
        
        [_gradeLab updateLayout];
    }
    return _gradeLab;
}

- (UIView *)starView
{
    if (!_starView)
    {
        _starView = [[UIView alloc] init];
    }
    return _starView;
}

- (UIImageView *)startImage
{
    if (!_startImage)
    {
        _startImage = [[UIImageView alloc] init];
    }
    return _startImage;
}

- (UILabel *)startLab
{
    if (!_startLab)
    {
        _startLab = [[UILabel alloc] init];
    }
    return _startLab;
}

@end
